<?php
class ViewController extends Controller {

	/**
	 * 相册页
	 *
	 * @param int $id        	
	 */
	public function actionIndex ($id) {
		$aid = (int) $id;
		$model = 'Album';
		if (!($album = CActiveRecord::model($model)->findByPk($aid)))
			$this->showError('没有这个相册呀!');
		
		$photoModel = 'AlbumPhoto';
		$albumDataProvider = new CActiveDataProvider($photoModel, array(
			'criteria' => array(
				'condition' => 'aid=' . $aid,
				'order' => 'lastUpdateTime DESC' 
			),
			'pagination' => array(
				'pageSize' => 40 
			) 
		));
		$commentModel = 'AlbumComment';
		$comment = new $commentModel('album');
		
		// 相册评论
		if (isset($_POST[$commentModel])) {
			$comment->attributes = $_POST[$commentModel];
			if ($album->addComment($comment)) {
				return $this->showSuccess('评论成功');
			} else
				return $this->showError($comment->getErrors());
		}
		
		$commentDataProvider = new CActiveDataProvider($commentModel, array(
			'criteria' => array(
				'condition' => 'aid=' . $aid,
				'order' => 'lastUpdateTime DESC' 
			),
			'pagination' => array(
				'pageSize' => 20 
			) 
		));
		if (Yii::app()->request->getIsAjaxRequest())
			$this->renderPartial('_albumComment', array(
				'commentDataProvider' => $commentDataProvider 
			));
		else
			$this->render('index', array(
				'aid' => $aid,
				'album' => $album,
				'isAuthor' => $this->module->checkIdentity($aid, 'author'),
				'comment' => $comment,
				'albumDataProvider' => $albumDataProvider,
				'commentDataProvider' => $commentDataProvider 
			));
	}

	/**
	 * 相册图片页
	 */
	public function actionPhoto ($id) {
		$pid = (int) $id;
		$photoModel = 'AlbumPhoto';
		if (!($photo = CActiveRecord::model($photoModel)->findByPk($pid)))
			$this->showError('图片不存在哟!');
		
		$model = 'Album';
		if (!($album = CActiveRecord::model($model)->findByPk($photo->aid)))
			$this->showError('没有这个相册呀!');
		$commentModel = 'AlbumComment';
		$comment = new $commentModel('photo');
		
		// 图片评论
		if (isset($_POST[$commentModel])) {
			$comment->attributes = $_POST[$commentModel];
			if ($photo->addComment($comment))
				return $this->showSuccess('评论成功');
			else
				return $this->showError($comment->getErrors());
		}
		
		$commentDataProvider = new CActiveDataProvider($commentModel, array(
			'criteria' => array(
				'condition' => 'pid=' . $pid,
				'order' => 'lastUpdateTime DESC' 
			),
			'pagination' => array(
				'pageSize' => 1 
			) 
		));
		if (Yii::app()->request->getIsAjaxRequest())
			$this->renderPartial('_photoComment', array(
				'commentDataProvider' => $commentDataProvider 
			));
		else
			$this->render('photo', array(
				'aid' => $photo->aid,
				'photo' => $photo,
				'pid' => $pid,
				'album' => $album,
				'isAuthor' => $this->module->checkIdentity($photo->aid, 'author'),
				'comment' => $comment,
				'commentDataProvider' => $commentDataProvider 
			));
	}
}
?>